package com.example.demo.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.example.demo.mapper.UserMapper;
import com.example.demo.pojo.User;
import com.example.demo.pojo.request.BaseRequest;
import com.example.demo.pojo.request.UserPageRequest;
import com.example.demo.service.IuserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;


@Service
public class userServiceImpl implements IuserService {
    @Autowired
    private UserMapper userMapper;
    //查询全部
    @Override
    public List<User> list() {
        List<User> userList = userMapper.list();
        return userList;
    }

    //分页查询
    @Override
    public PageInfo<User> page(UserPageRequest userPageRequest) {
        PageHelper.startPage(userPageRequest.getPageNum(),userPageRequest.getPageSize());
        List<User> userList = userMapper.page(userPageRequest);
       return new PageInfo<>(userList);
    }

    //添加数据
    @Override
    public void save(User user) {
        Date date = new Date();
        user.setIdCard(DateUtil.format(date,"yyyyMMdd")+Math.abs(IdUtil.fastSimpleUUID().hashCode()));
        userMapper.save(user);
    }

    //回写信息
    @Override
    public User getUser(Integer id) {
         User user = userMapper.getUser(id);
         return user;
    }

    //编辑数据
    @Override
    public void getIDUser(User user) {
        user.setUpdateTime(new Date());
        userMapper.getUserByID(user);
    }

    //删除数据
    @Override
    public void delete(Integer id) {
        userMapper.delete(id);
    }


}
